package com.guarantee.server.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.guarantee.server.common.utils.DateUtils;
import com.guarantee.server.domain.FoodElemeServer;
import com.guarantee.server.domain.Order;
import com.guarantee.server.service.IFoodElemeServerService;
import com.guarantee.server.service.IOrderService;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @Author huisonglin
 * @Description
 * @PackageName outfood-platform
 * @Package com.outfood.task
 * @Date 2024-03-25 17:08
 * @Version 1.0
 */
@Component
@Log4j2
public class ScanElemeOrderTask {


    @Resource
    IOrderService orderService;


    @Autowired
    ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Resource
    RedisTemplate redisTemplate;

    @Resource
    IFoodElemeServerService foodElemeServerService;




    @Scheduled(cron = "0/10 * * * * ? ")
    public void toOut(){
        FoodElemeServer foodElemeServer = foodElemeServerService.selectById(1L);
        if(foodElemeServer.getUseLocalPc() == 0 ){
            log.info("本机不用出餐，使用线上服务器出餐开始使用定时器出餐");
            return;
        }{
            log.info("正在使用自家电脑进行出餐");
        }
        long start  = new Date().getTime();
        log.info("------------------------------------------开始出餐------------------------------------------------------------");
        String time = DateUtils.getTime();
        LambdaQueryWrapper<Order> query = Wrappers.<Order>lambdaQuery();
        query.eq(Order::getOrderSource, "ELEME");
        query.eq(Order::getOutStatus, "READY");
        query.lt(Order::getTheoryOutTime, DateUtils.getTime());
        List<Order> list = orderService.selectList(query);
        list.forEach(order -> {
            Boolean result = redisTemplate.opsForValue().setIfAbsent("out:" + order.getId(), "1", 300, TimeUnit.SECONDS);
            if(!result){
                return;
            }
            order.setOutStatus("PROCESSING");
            orderService.updateById(order);
            threadPoolTaskExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        orderService.out(order);
                    } catch (Exception e) {
                        log.info(e.getMessage(),e);
                        order.setOutStatus("EXCEPT");
                        orderService.updateById(order);
                    }
                }
            });

        });
        long end  = new Date().getTime();
        log.info("------------------------------------------出餐结束共耗时{}ms------------------------------------------------------------",end-start);
    }



}
